package com.graduate.work.graduatework.Controller;

import com.graduate.work.graduatework.common.Result;
import com.graduate.work.graduatework.common.ResultStatus;
import com.graduate.work.graduatework.entity.AirTicketDetail;
import com.graduate.work.graduatework.entity.User;
import com.graduate.work.graduatework.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;

/**
 * @author: cuibingqi.
 * @date: 17/5/4 下午9:11.
 * @descripe:
 */

@Controller
@RequestMapping("search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * 低价搜索接口
     * @param depCity
     * @param arrCity
     * @param depDate
     * @return
     */
    @RequestMapping(value = "lowsearch",method = RequestMethod.GET)
    @ResponseBody
    public String lowSearch(String depCity, String arrCity, String depDate,ModelMap modelMap){
        Result<List<AirTicketDetail>> result = new Result<>(ResultStatus.SUCCESS.getCode(),ResultStatus.SUCCESS.getMsg());
        Optional<String> isdepCity = Optional.ofNullable(depCity);
        Optional<String> isarrCity = Optional.ofNullable(arrCity);
        Optional<String> isdepDate = Optional.ofNullable(depDate);
        if(!isdepCity.isPresent()||!isarrCity.isPresent()||!isdepDate.isPresent()){
            result.setCode(ResultStatus.FAIL.getCode());
            result.setMsg(ResultStatus.FAIL.getMsg());
            return "error";
        }
        result.setBody(searchService.OWSearch(depCity,arrCity,depDate));
        modelMap.addAttribute("searchResult",result);

        return "searchResult";
    }


}
